package io.gbznt.modules.buss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.gbznt.common.utils.Constant;
import io.gbznt.common.utils.ShiroUtils;
import io.gbznt.modules.buss.entity.BussFarmlandInfoEntity;
import io.gbznt.modules.buss.entity.BussRtuInfoEntity;
import io.gbznt.modules.buss.service.BussFarmlandInfoService;
import io.gbznt.modules.buss.service.BussRtuInfoService;
import io.gbznt.modules.sys.service.SysDeptService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Console;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.Query;

import io.gbznt.modules.buss.dao.BussRtuEquipmentDao;
import io.gbznt.modules.buss.entity.BussRtuEquipmentEntity;
import io.gbznt.modules.buss.service.BussRtuEquipmentService;


@Service("bussRtuEquipmentService")
public class BussRtuEquipmentServiceImpl extends ServiceImpl<BussRtuEquipmentDao, BussRtuEquipmentEntity> implements BussRtuEquipmentService {

    @Autowired
    private BussFarmlandInfoService bussFarmlandInfoService;
    @Autowired
    private BussRtuInfoService bussRtuInfoService;
    @Autowired
    private SysDeptService sysDeptService;
    

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String name = (String)params.get("name");
        //田块id
        String farmlandId=(String)params.get("farmlandId");
        Set<String> rtuIds = new HashSet<>();

        if(StringUtils.isNotEmpty(farmlandId)){
            //获取rtu信息
            QueryWrapper<BussRtuInfoEntity> ew =new QueryWrapper<BussRtuInfoEntity>();
            ew.eq(StringUtils.isNotEmpty(farmlandId),"farmland_info_id", farmlandId);
            List<BussRtuInfoEntity> rtuList=bussRtuInfoService.list(ew);

            for (BussRtuInfoEntity bussRtuInfoEntity : rtuList) {
                rtuIds.add(bussRtuInfoEntity.getId());
            }
        }

        IPage<BussRtuEquipmentEntity> page = this.page(
                new Query<BussRtuEquipmentEntity>().getPage(params),
                new QueryWrapper<BussRtuEquipmentEntity>()
                     .like(StringUtils.isNotEmpty(name),"name",name)
                     .in(StringUtils.isNotEmpty(farmlandId),"rtu_id", rtuIds)
                    // 个人数据权限控制
                    .in(!Constant.SUPER_ADMIN.equals(ShiroUtils.getUserId()),
                            "dept_id", sysDeptService.getPermissionDeptIdsArr(true,true))
                    .orderByDesc("create_time")
        );

        for (BussRtuEquipmentEntity bussRtuEquipmentEntity: page.getRecords()) {
            BussRtuInfoEntity bussRtuInfoEntity = bussRtuInfoService.getById(
                    bussRtuEquipmentEntity.getRtuId());
            if(null != bussRtuInfoEntity) {
                bussRtuEquipmentEntity.setRtuSerialNum(bussRtuInfoEntity.getSerialNum());

                BussFarmlandInfoEntity bussFarmlandInfoEntity = bussFarmlandInfoService.getById(
                        bussRtuInfoEntity.getFarmlandInfoId());
                if(null!=bussFarmlandInfoEntity){
                    bussRtuEquipmentEntity.setRtuFarmlandName(bussFarmlandInfoEntity.getName());
                }


            }

        }

        return new PageUtils(page);
    }

}
